﻿using System;
using System.Collections.Generic;
using System.Drawing;
using NUnit.Framework;
using Smartkernel;
using Smartkernel.Attributes;
using Smartkernel.Xml;

namespace Smartkernel.NUnitTest.Xml
{
    [Category("Xml")]
    [TestFixture]
    public class SmartXmlTest
    {
        [Test]
        public void Test()
        {
            //【01】根据特性标识映射
            string xmlPath1 = Environment.CurrentDirectory + @"\DemoConfigerEntity.xml";
            DemoConfigerEntity demoConfigerEntity1 = SmartXml.MappingXO<DemoConfigerEntity>(xmlPath1, SmartMappingType.Attribute);
            Assert.AreEqual(demoConfigerEntity1.Name, "SmartKernel");
            Assert.AreEqual(demoConfigerEntity1.BirthDay, DateTime.Parse("1982-05-02 12:00"));
            Assert.AreEqual(demoConfigerEntity1.QQ, 120018689);
            Assert.AreEqual(demoConfigerEntity1.Money, 123.45m);
            Assert.AreEqual(demoConfigerEntity1.EMails.Count, 3);
            Assert.AreEqual(demoConfigerEntity1.LoveDay, DayOfWeek.Monday);
            Assert.AreEqual(demoConfigerEntity1.LoveColor == null, false);
            Assert.AreEqual(demoConfigerEntity1.LoveSong == null, true);
            Assert.AreEqual(demoConfigerEntity1.LoveBook, "史记");

            //【02】根据字段映射
            DemoConfigerEntity demoConfigerEntity2 = SmartXml.MappingXO<DemoConfigerEntity>(xmlPath1, SmartMappingType.Field);
            Assert.AreEqual(demoConfigerEntity2.Name, "SmartKernel");
            Assert.AreEqual(demoConfigerEntity2.BirthDay, DateTime.Parse("1982-05-02 12:00"));
            Assert.AreEqual(demoConfigerEntity2.QQ, 120018689);
            Assert.AreEqual(demoConfigerEntity2.Money, 123.45m);
            Assert.AreEqual(demoConfigerEntity2.EMails.Count, 3);
            Assert.AreEqual(demoConfigerEntity2.LoveDay, DayOfWeek.Monday);
            Assert.AreEqual(demoConfigerEntity2.LoveColor == null, false);
            Assert.AreEqual(demoConfigerEntity2.LoveSong == null, true);
            Assert.AreEqual(demoConfigerEntity2.LoveBook == null, true);

            //【03】根据属性映射
            DemoConfigerEntity demoConfigerEntity3 = new DemoConfigerEntity();
            SmartXml.MappingXO(xmlPath1, SmartMappingType.Property, ref demoConfigerEntity3);
            Assert.AreEqual(demoConfigerEntity3.Name, "SmartKernel");
            Assert.AreEqual(demoConfigerEntity3.BirthDay, DateTime.Parse("1982-05-02 12:00"));
            Assert.AreEqual(demoConfigerEntity3.QQ, 120018689);

            //【04】映射列表
            string xmlPath2 = Environment.CurrentDirectory + @"\DemoListConfigerEntity.xml";
            List<DemoConfigerEntity> list = SmartXml.MappingXOList<DemoConfigerEntity>(xmlPath2, SmartMappingType.Attribute);
            Assert.AreEqual(list.Count, 5);
            list.ForEach(entity => Assert.AreEqual(entity.Name.StartsWith("SmartKernel"), true));
        }

        private class DemoConfigerEntity
        {
            private string name;
            [SmartXmlMapping("Name")]
            public string Name
            {
                get { return this.name; }
                set { this.name = value; }
            }

            [SmartXmlMapping("BirthDay")]
            private DateTime birthDay;
            public DateTime BirthDay
            {
                get { return this.birthDay; }
                set { this.birthDay = value; }
            }

            private int qq;
            [SmartXmlMapping("QQ")]
            public int QQ
            {
                get { return this.qq; }
                set { this.qq = value; }
            }

            [SmartXmlMapping("Money")]
            private decimal money;
            public decimal Money
            {
                get { return this.money; }
                set { this.money = value; }
            }

            private List<string> emails;
            [SmartXmlMapping("EMails")]
            public List<string> EMails
            {
                get { return this.emails; }
                set { this.emails = value; }
            }

            [SmartXmlMapping("LoveColor")]
            private Color loveColor;
            public Color LoveColor
            {
                get { return this.loveColor; }
                set { this.loveColor = value; }
            }

            private DayOfWeek loveDay;
            [SmartXmlMapping("LoveDay")]
            public DayOfWeek LoveDay
            {
                get { return this.loveDay; }
                set { this.loveDay = value; }
            }

            private string loveSong;
            [SmartXmlMapping("LoveSong")]
            public string LoveSong
            {
                get { return this.loveSong; }
                set { this.loveSong = value; }
            }

            private string loveBook;
            [SmartXmlMapping("LoveBook", DefaultValue = "史记")]
            public string LoveBook
            {
                get { return this.loveBook; }
                set { this.loveBook = value; }
            }
        }
    }
}
